#!/bin/env python
# -*- coding:utf-8 -*-

from rediscluster import StrictRedisCluster
from lib.utils import ConfigSectionMap


class AppRedis():
    def __init__(self, connection):
        self.rs = StrictRedisCluster(startup_nodes=connection, decode_responses=True, max_connections=200)
        self.ttl = 3600
        self.perfix = config.get('CHECK_PERFIX', 'tw_alert_')

    def get_key(self, key):
        key = self.perfix + key
        return self.rs.get(key)

    def set_key(self, key, value, ttl=60):
        key = self.perfix + key
        self.rs.set(key, value)
        self.rs.expire(key, ttl)

    def append(self, key, value):
        key = self.perfix + key

config = ConfigSectionMap("REDIS")
connection_pool = [{"host": host.split(":")[0], "port": host.split(":")[1]} for host in config.get("host").split(",")]

redis = AppRedis(connection_pool)
